package org.eyeTopLight.controller;

import org.eyeTopLight.po.CommonResult;
import org.eyeTopLight.po.initEntity.Goods;
import org.eyeTopLight.service.AccessoryGoodsService;
import org.eyeTopLight.service.FrameGoodsService;
import org.eyeTopLight.service.LenGoodsService;
import org.eyeTopLight.service.SunglassesGoodsService;
import org.eyeTopLight.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
public class GoodController {


    private static final Logger logger = LoggerFactory.getLogger(ImageController.class);

    @Resource
    FrameGoodsService frameGoodsService;
    @Resource
    LenGoodsService lenGoodsService;

    @Resource
    SunglassesGoodsService sunglassesGoodsService;

    @Resource
    AccessoryGoodsService accessoryGoodsService;

    /**（待修改）
     * 分页获取某个指定分类下的一页的商品，用于展示
     * @param goods_category symbol下的一个分类
     * @param goods_symbol 表示frame,len,sunglasses,accessory中的一个
     * @param startIndex
     * @param maxRows
     * @return 所有的分类商品都在这里返回，需要新建一个VO类，包含goods_id,goods_name,goods_symbol三个字段（VO类表示与前端交互的类）
     */
    @GetMapping(value="/goods/getGoods")
    public CommonResult getGoods(@RequestParam(value = "goods_category",required = false,defaultValue = "all") String goods_category,
                                @RequestParam(value = "goods_symbol", required = false,defaultValue = "frame") String goods_symbol,
                                @RequestParam(value = "startIndex",required = false,defaultValue = "0") Integer startIndex,
                                @RequestParam(value = "count",required = false,defaultValue = "10") Integer maxRows) {

        try{
            List<GoodsVo> resultList = null;
            if (goods_symbol.equals("frame")) {
                resultList = frameGoodsService.getGoods(goods_category,startIndex,maxRows);
            } else if (goods_symbol.equals("len")) {
                resultList = lenGoodsService.getGoods(goods_category,startIndex,maxRows);
            } else if (goods_symbol.equals("len")) {
                resultList = sunglassesGoodsService.getGoods(goods_category,startIndex,maxRows);
            } else {
                resultList = accessoryGoodsService.getGoods(goods_category,startIndex,maxRows);
            }
            return new CommonResult(200,"success",resultList);
        }
        catch (Exception e){
            logger.error("数据库Goods表操作失败");
            return new CommonResult(500,"服务器内部错误",e);
        }
    }

    /**
     * (待修改）
     * @param goods_symbol 商品所属大分类
     * @param goods_id 对应的id
     * @return 此处需要拆分为四个方法，因为涉及到不同的产品，需要拆分为四个方法
     */
    @GetMapping(value="/goods/getOneGood")
    public CommonResult getOneGood(@RequestParam(value = "goods_symbol", required = false,defaultValue = "frame") String goods_symbol,
                                 @RequestParam(value = "goods_id",required = false,defaultValue = "0") Integer goods_id) {
        try{
            Goods good = goods_symbol.equals("frame") ? frameGoodsService.getOneGood(goods_symbol,goods_id) :
                    lenGoodsService.getOneGood(goods_symbol,goods_id);
            return new CommonResult(200, "success",good);
        }
        catch (Exception e){
            logger.error("数据库Goods表操作失败");
            return new CommonResult(500,"服务器内部错误",e);
        }
    }


    @GetMapping(value = "/goods/getTopNGoods")
    public CommonResult<List<GoodsVo>> getTopNGoods(@RequestParam(value = "goods_symbol", required = false, defaultValue = "frame") String goods_symbol,
                                              @RequestParam(value = "count", required = false, defaultValue = "10") Integer count){
        List<GoodsVo> topNGoodsList = null;
        if (goods_symbol.equals("frame")){
            topNGoodsList = frameGoodsService.getTopNGoods(count);
        } else if (goods_symbol.equals("len")) {
            topNGoodsList = lenGoodsService.getTopNGoods(count);
        } else if (goods_symbol.equals("sunglasses")) {
            topNGoodsList = sunglassesGoodsService.getTopNGoods(count);
        } else {
            topNGoodsList = accessoryGoodsService.getTopNGoods(count);
        }
        return new CommonResult<List<GoodsVo>>(200, "success", topNGoodsList);
    }
}
